import json
import time

from ScrapyObject.spiders.utils.aa import read_json_file, spiderInfo, VideoBean, read_json_file_two

# 2把不能播放的数据删除并把最终结果保存到本地
if __name__ == '__main__':
    print('开始')
    timestamp = int(time.time())
    if spiderInfo.isPlay:
        # jsonStr = read_json_file('D:\\' + spiderInfo.name + '1.json')
        jsonStr = read_json_file('../../../' + spiderInfo.name + '1.json')
        print("原始数据一共: " + str(len(jsonStr)))
        videoList = []
        for i in jsonStr:
            video = VideoBean()
            video.id = 0
            video.name = i['name']
            video.url = i['url']
            video.tags = i['tags']
            video.pUrl = i['pUrl']
            video.vUrl = i['vUrl']
            videoList.append(video)
        jsonStr.clear()
        test_list = list(set(videoList))
        print("原始数据去重后一共: " + str(len(test_list)))
        jsonStr = read_json_file_two('../../../' + spiderInfo.name + 'Text.json')
        print("原始数据一共: " + str(len(jsonStr)))
        for videoUrlData in test_list:
            for pictureUrlData in jsonStr:
                if videoUrlData.vUrl == pictureUrlData['vUrl']:
                    videoUrlData.i = "1"
        for num in reversed(test_list):
            if num.i != "1":
                test_list.remove(num)
        for videoUrlData in test_list:
            videoUrlData.url = ""
        videoFinalList = []
        for i in test_list:
            tinydict = {'id': 0, 'name': i.name, 'url': '', 'tags': i.tags, 'pUrl': i.pUrl, 'vUrl': i.vUrl}
            videoFinalList.append(tinydict)
        test_list.clear()
        print("最终数据一共: " + str(len(videoFinalList)))
        # 将JSON数组转换为字符串
        json_string = json.dumps(videoFinalList, ensure_ascii=False)
        # 将字符串写入txt文件
        with open('D:\\' + spiderInfo.name + '.json', 'w', encoding='utf-8') as file:
            file.write(json_string)
    endTime = int(time.time())
    print(str(endTime - timestamp))
    print('结束')
